#include "application.h"
#include "Ap_photo_capture.h"

#if C_SPI_FLASH_DISK
float UPGRADE_BIN_SIZE	= 1024 + 64;//2400;//1958+30;//920	//means the "gp_cardvr_upgrade.bin" total size	 (unit KB) 
float OVER_PROVISIONING	= 30;	//means reserve some space, In case the gp_cardvr_upgrade.bin is too large (unit KB)
float SPI_FLASH_TOTAL_SIZE = 2;	//means the capacity of the SPI FLASH CHIP (unit MB)
INT32U CLOCK_SPEED=24000000;//36000000;
#endif


static STR_ICON_EXT capture_resolution_str;
static STR_ICON_EXT left_capture_num_str;
//add
static STR_ICON_EXT capture_file_name_str;


#define CAPTURE_SHOW_FILE_NAME_BUFF_W 310
#define CAPTURE_SHOW_FILE_NAME_BUFF_H  22
INT32U capture_show_file_name_buff = NULL;

//INT8U capture_show_file_name_enble = 0;
INT16U capture_show_file_cnt_start = 0;

static INT32U
	pic_size[3][8] = { //unit: KB
		{ 1410, 1220, 1030, 753, 529, 385, 227, 88 }, //fine
		{ 1120, 996, 844, 603, 433, 310, 195, 79 }, //normal
		{ 810, 692, 577, 396, 273, 142, 95, 43 } //economy
	};

INT32U	last_pic_size = 0;	//unit: KB
INT32U	last_pic_size_temp = 0;

INT32U	peri_video_resolution = 0;

INT32U	left_capture_num = 0;

Overlap_Capture_Function Capture;

void overlap_capture_function_initial(void)
{
	#if C_TOUCH_PANEL
	Capture.ap_video_preview_init_ptr = &ap_capture_init_TP;
	Capture.ap_video_preview_exit_ptr = &ap_capture_exit_TP;
	#else
	Capture.ap_video_preview_init_ptr = &ap_capture_init;
	Capture.ap_video_preview_exit_ptr = &ap_capture_exit;
	#endif
}

#if C_TOUCH_PANEL == CUSTOM_OFF
#if C_SPI_FLASH_DISK
extern void index_spi_file();
#endif
void ap_capture_init(void)
{
	INT8U	temp;

	OSQPost(DisplayTaskQ, (void *) MSG_DISPLAY_TASK_EFFECT_INIT);
	ap_display_show_date_time(SHOW_TIME_RECORD);
	if(ap_state_handling_storage_id_get() == NO_STORAGE)
	{
		//OSQPost(DisplayTaskQ, (void *) (MSG_DISPLAY_TASK_LEFT_REC_TIME_DRAW | 0));
		ap_state_handling_icon_show_cmd(ICON_CAPTURE, ICON_INTERNAL_MEMORY, 0);
	}
	else
	{
		//video_calculate_left_recording_time_enable();
		ap_state_handling_icon_show_cmd(ICON_CAPTURE, ICON_SD_CARD, 0);
	}

	//ap_capture_resolution_display();
#if C_SPI_FLASH_DISK
	index_spi_file();
#endif
	left_capture_num = ap_capture_left_num_calculate();
	ap_capture_left_num_display(left_capture_num);

/*
	temp = ap_state_config_anti_shaking_get();
	if(temp)
	{
		ap_state_handling_icon_show_cmd(ICON_CAPTURE_ANTI_SHAKING, 0, 0);
	}

	temp = ap_state_config_burst_get();
	if(temp)
	{
		ap_state_handling_icon_show_cmd(ICON_CAPTURE_SEQUENCE, 0, 0);
	}

	temp = ap_state_config_quality_get();
	ap_state_handling_icon_show_cmd(ICON_CAPTURE_QUALITY_FINE + temp, 0, 0);
*/

	if (capture_show_file_name_buff == NULL)
	{
		capture_show_file_name_buff = (INT32U)gp_malloc_align(CAPTURE_SHOW_FILE_NAME_BUFF_W * CAPTURE_SHOW_FILE_NAME_BUFF_H * 2, 64);
		if (!capture_show_file_name_buff)
		{
			DBG_PRINT("capture init malloc capture_show_file_name_buff fail.... in function [%s] in file [%s] in line[%d]\r\n", __FUNCTION__, __FILE__, __LINE__);
		}
	}

	temp = ap_state_config_pic_size_get();
	if (temp == 0)
	{
		ap_state_handling_icon_show_cmd(ICON_ONE_M, 0, 0);
	}
	else if (temp == 1)
	{
		ap_state_handling_icon_show_cmd(ICON_TWO_M, 0, 0);
	}
	else if (temp == 2)
	{
		ap_state_handling_icon_show_cmd(ICON_THREE_M, 0, 0);
	}

#if C_BATTERY_DETECT == CUSTOM_ON
	ap_state_handling_current_bat_lvl_show();
	ap_state_handling_current_charge_icon_show();
#endif

}

void ap_capture_exit(void)
{
#if (_SENSOR_CSI_FRONT == 0) && (C_REAR_ONLY == CUSTOM_OFF)
	INT32U temp1;
#endif
	capture_show_file_cnt_start = 0;

	if (capture_show_file_name_buff)
	{
		gp_free((void*)capture_show_file_name_buff);
		capture_show_file_name_buff = NULL;
	}
	
	ap_state_handling_str_draw_exit1();
	ap_state_handling_str_draw_exit();
	ap_capture_resolution_clear();
	ap_capture_left_num_clear();
	//ap_capture_file_name_clear();
	ap_display_show_date_time(SHOW_TIME_NONE);
	video_calculate_left_recording_time_disable();
#if (_SENSOR_CSI_FRONT == 0) && (C_REAR_ONLY == CUSTOM_OFF)
	temp1 = ap_state_config_white_balance_get();
	if(temp1 != 0)
	{ 
		//DBG_PRINT ("capture reset awbmode to auto!!!\r\n");
		drv_l2_cdsp_set_awb_mode(AWB_AUTO_DC);
	}
#endif 

	//ap_state_handling_icon_clear_cmd(ICON_CAPTURE, 0, 0);
}



void ap_capture_cdsp_config(void)
{
#if (_SENSOR_CSI_FRONT == 0) && (C_REAR_ONLY == CUSTOM_OFF)
	INT8U	temp; 
	/*ev*/
	temp = ap_state_config_ev1_get();
	ap_state_handling_icon_show_cmd(ICON_CAPTURE_EV6 + temp, 0, 0);
	drv_l2_cdsp_set_ev_val(temp);
	//0:+2, 1:+5/3, 2:+4/3, 3:+1.0, 4:+2/3, 5:+1/3, 6:+0.0, 7:-1/3, 8:-2/3, 9:-1.0, 10:-4/3, 11:-5/3, 12:-2.0
	/*awb*/
	temp = ap_state_config_white_balance_get();
	ap_state_handling_icon_show_cmd(ICON_CAPTURE_WHITE_BALANCE_AUTO + temp, 0, 0);

	if(temp !=0)
	{
		drv_l2_gp_cdsp_set_awb_by_config(temp);
	}

	/*iso*/
	temp = ap_state_config_iso_get();
	ap_state_handling_icon_show_cmd(ICON_CAPTURE_ISO_AUTO + temp, 0, 0);
	drv_l2_cdsp_set_iso(temp);
#endif // #if (_SENSOR_CSI_FRONT == 0)

}

#endif // #if C_TOUCH_PANEL == CUSTOM_OFF

extern INT16S SDC_ERR;
INT32S ap_capture_func_key_active(void)
{

	#if 1
	if(SDC_ERR)
	{
		ap_state_handling_str_draw(STR_SDC_ERR, WARN_STR_COLOR);
		ap_state_handling_str_draw1(STR_PROMPT, WARN_STR_COLOR);
		msgQSend(PeripheralTaskQ, MSG_PERIPHERAL_TASK_DISPLAY_PLEASE_INSERT_SDC, NULL, 0, MSG_PRI_NORMAL);
		DBG_PRINT("SDC error,please format\r\n");
		return STATUS_FAIL;
	}
	#endif
	
	#if C_SPI_FLASH_DISK
		DBG_PRINT(" SPI FLASH STORAGE \r\n");
	#else
	if(ap_state_handling_storage_id_get() == NO_STORAGE)
	{
		#if(defined(C_ZHONGQIN_SPECIAL) && (C_ZHONGQIN_SPECIAL == CUSTOM_ON))
		ap_state_handling_str_draw(STR_NO_SD, WARN_STR_COLOR);
		#else
		ap_state_handling_str_draw(STR_INSERT_SDC, WARN_STR_COLOR);
		ap_state_handling_str_draw1(STR_PROMPT, WARN_STR_COLOR);
		#endif
		
		msgQSend(PeripheralTaskQ, MSG_PERIPHERAL_TASK_DISPLAY_PLEASE_INSERT_SDC, NULL, 0, MSG_PRI_NORMAL);
		return STATUS_FAIL;
	}
	#endif
	
	
	#if C_SPI_FLASH_DISK
	if(ap_state_handling_storage_id_get() != NO_STORAGE) {
	#endif
		
		if((vfsFreeSpace(STORAGE_DEVICE) >> 20) < CARD_FULL_MB_SIZE)
		
		{
			DBG_PRINT("Card full, key action avoid!!!\r\n");

			ap_state_handling_str_draw(STR_SD_FULL, WARN_STR_COLOR);
			ap_state_handling_str_draw1(STR_PROMPT, WARN_STR_COLOR);
			msgQSend(StorageServiceQ, MSG_STORAGE_SERVICE_TIMER_START, NULL, 0, MSG_PRI_NORMAL);

			return STATUS_FAIL;
		}
	#if C_SPI_FLASH_DISK	
	} else 
	{	
	DBG_PRINT("zzy---1 time=%d\r\n", OSTimeGet());
			//DBG_PRINT("SPI FLASH reamin size:%llu\r\n",vfsFreeSpace(FS_NOR));
			DBG_PRINT("zzy---2 time=%d\r\n", OSTimeGet());
			//DBG_PRINT("******CARD_FULL_MB_SIZE*10:%d\r\n",CARD_FULL_MB_SIZE*10);
			//DBG_PRINT("******CARD_FULL_MB_SIZE*10 :%llu\r\n",CARD_FULL_MB_SIZE*10);
			//if (((vfsFreeSpace(FS_NOR) >> 10)-(2048-M_R_SIZE))< CARD_FULL_MB_SIZE*10) { //150k
			/*
		if ((vfsFreeSpace(FS_NOR) >> 10) < CARD_FULL_MB_SIZE*10) { //150k
	        	DBG_PRINT ("SPI FLASH storage full, key action avoid!!!\r\n"); 
			ap_state_handling_str_draw(STR_SD_FULL, WARN_STR_COLOR);
			msgQSend(StorageServiceQ, MSG_STORAGE_SERVICE_TIMER_START, NULL, 0, MSG_PRI_NORMAL);
			
	        	return	STATUS_FAIL;
		}  
		*/
	} 
	#endif	



	msgQSend(StorageServiceQ, MSG_STORAGE_SERVICE_PIC_REQ, NULL, 0, /*MSG_PRI_NORMAL*/MSG_PRI_URGENT);
	return STATUS_OK;
}

INT32S ap_capture_reply_action(STOR_SERV_FILEINFO *file_info_ptr)
{
	MEDIA_SOURCE	src;

	src.type_ID.FileHandle = file_info_ptr->file_handle;
	src.type_ID.FileHandle_b = file_info_ptr->file_handle_b;
	src.type = SOURCE_TYPE_FS;
	src.Format.VideoFormat = MJPEG;

	if(src.type_ID.FileHandle >= 0)
	{
		// fix it, modify it if play audio abnormail
		//audio_effect_wait();

		if(video_encode_capture_picture(src) == STATUS_OK)
		{
			return STATUS_OK;
		}
		else
		{
			close(file_info_ptr->file_handle);
			unlink((CHAR *) file_info_ptr->file_path_addr);
			if(file_info_ptr->file_handle_b >= 0)
			{
				close(file_info_ptr->file_handle_b);
				unlink((CHAR *) file_info_ptr->file_path_addr_b);
			}

			return STATUS_FAIL;
		}
	}
	else
	{
		ap_state_handling_str_draw(STR_NO_SD, WARN_STR_COLOR);
		ap_state_handling_str_draw1(STR_PROMPT, WARN_STR_COLOR);
		msgQSend(PeripheralTaskQ, MSG_PERIPHERAL_TASK_DISPLAY_PLEASE_INSERT_SDC, NULL, 0, MSG_PRI_NORMAL);
		msgQSend(StorageServiceQ, MSG_STORAGE_SERVICE_TIMER_START, NULL, 0, MSG_PRI_NORMAL);
		return STATUS_FAIL;
	}
}

void ap_capture_reply_done(INT8U ret, INT32U file_path_addr)
{
	struct stat_t buf_tmp;
	INT32U	temp;
	INT32S	temp1;
	INT64U	free_space;

#if C_SPI_FLASH_DISK
	if (ap_state_handling_storage_id_get() != NO_STORAGE){ //SD 
#endif

	UsrFlushBuffers(STORAGE_DEVICE);

	#if C_CREATE_FILE_DEBUG
	FatSpeedUpPrintf(STORAGE_DEVICE);
	DBG_PRINT("\r\n");
	#endif
	
#if 1
	stat((CHAR *) file_path_addr, &buf_tmp);
	temp = buf_tmp.st_size >> 10;
	temp1 = temp - last_pic_size_temp;
	last_pic_size_temp = last_pic_size_temp + temp1;
	DBG_PRINT("Capture OK, Size=%d kbtye\r\n", buf_tmp.st_size >> 10);

	free_space = vfsFreeSpace(STORAGE_DEVICE);
	DBG_PRINT("free_space(Mbyte)=%d \r\n", (free_space >> 20) - CARD_FULL_MB_SIZE);
	if((free_space >> 20) < 20)			//<20Mbyte
	{
		DBG_PRINT("left_capture_num old=%d \r\n", left_capture_num);
		left_capture_num = ap_capture_left_num_calculate();
		DBG_PRINT("left_capture_num=%d \r\n", left_capture_num);
	}
	else
	{
		if(left_capture_num)
		{
			left_capture_num--;
		}
	}
#endif
	ap_capture_left_num_display(left_capture_num);
#if !C_SENSOR_SAVE_RAW_DATA
	msgQSend(StorageServiceQ, MSG_STORAGE_SERVICE_TIMER_START, NULL, 0, MSG_PRI_NORMAL);
#endif

#if C_SPI_FLASH_DISK
	} else { //SPI FLASH
	
		UsrFlushBuffers(FS_NOR);
		stat((CHAR *) file_path_addr, &buf_tmp);
		temp = buf_tmp.st_size >> 10;
		temp1 = temp - last_pic_size_temp;
		last_pic_size_temp = last_pic_size_temp+temp1;
		DBG_PRINT("SPI Capture OK, Size=%d kbtye\r\n",buf_tmp.st_size>>10);

		//free_space = vfsFreeSpace(FS_NOR);
		//DBG_PRINT("SPI Flash free_space(kbyte)=%d \r\n",(free_space>>10));

		
		if(0)//((free_space >> 10) < 30)			//<30Kb
		{
		
			//DBG_PRINT("left_capture_num old=%d \r\n", left_capture_num);
			left_capture_num = ap_capture_left_num_calculate();
			//DBG_PRINT("left_capture_num=%d \r\n", left_capture_num);
		}
		else
		{
			if(left_capture_num)
			{
				left_capture_num--;
			}
		}
		/*
		left_capture_num = ap_capture_left_num_calculate();
		ap_capture_left_num_display(left_capture_num);
		if(left_capture_num)
		{
			left_capture_num--;
		}
		*/
		ap_capture_left_num_display(left_capture_num);
		msgQSend(StorageServiceQ, MSG_STORAGE_SERVICE_TIMER_START, NULL, NULL, MSG_PRI_NORMAL);		
		

	}
#endif

	
	
}

#if C_SPI_FLASH_DISK
INT16U	g_spi_file_num;
//extern INT32S get_spi_file_index();
void index_spi_file()
{		
	INT32S  final_num;
	g_spi_file_num=0;
	//final_num=get_file_final_jpeg_index();	//获取最后一张照片
	//if(final_num==0)
	final_num=get_file_final_jpeg_index();	//get_file_final_jpeg_index();	//获取最后一张照片
	DBG_PRINT("spi flash file num = %d\r\n", final_num);
}
INT32U spi_photo_num_count()
{
	INT64U	freespace ;
	INT32U	left_num;
	INT32S  final_num;
	
	//_findfirst("*.jpg", &file_info, D_ALL);
	/*
	g_spi_file_num=0;
	final_num=get_file_final_jpeg_index();	//获取最后一张照片
	if(final_num==0)
		final_num=get_file_final_jpeg_index();	//获取最后一张照片
	*/
	
	DBG_PRINT("final_num:%d\r\n",final_num);

	DBG_PRINT("g_spi_file_num:%d\r\n",g_spi_file_num);
	if(g_spi_file_num>SPI_PHOTO_MAX_NUM)
			left_num=0;
	else
			left_num=SPI_PHOTO_MAX_NUM-g_spi_file_num;
	return 	left_num;
}

#endif

INT32U ap_capture_left_num_calculate(void)
{
	INT64U	freespace, left_num, temp_pic_size;

	if(ap_state_handling_storage_id_get() == NO_STORAGE)
	{	
		
		//DBG_PRINT("****TO this STEp!\r\n****");
		#if C_SPI_FLASH_DISK
		/*
		if(last_pic_size_temp)
		{
			last_pic_size = last_pic_size_temp;
		}

		freespace = vfsFreeSpace(FS_NOR);
		DBG_PRINT("****show SPI_FLASH rest size:%d\r\n",freespace>>10);
	
		if(freespace > ((INT32U)OVER_PROVISIONING << 10))//>30720
			freespace -= ((INT32U)OVER_PROVISIONING << 10);
		else
			freespace = 0;
		
		if(freespace == 0)
		{
			left_num = 0;
		}
		else
		{
			if(last_pic_size == 0)
				temp_pic_size = pic_size[ap_state_config_quality_get()][ap_state_config_pic_size_get()];
			else
				temp_pic_size = last_pic_size;

			DBG_PRINT("****current temp_pic_size:%d\r\n",temp_pic_size);
			left_num = (freespace >> 10) / temp_pic_size;

			if(left_num > 99999)
				left_num = 99999;
		}
		*/
			//index_spi_file();
			left_num=spi_photo_num_count();
		#else
			left_num = 0;
		#endif
	}
	else
	{
		//DBG_PRINT("**********************************\r\n");
		if(last_pic_size_temp)
		{
			last_pic_size = last_pic_size_temp;
		}

		freespace = vfsFreeSpace(STORAGE_DEVICE);
		//DBG_PRINT("****TO this STEP!vfsFreeSpace(STORAGE_DEVICE):%d\r\n****",vfsFreeSpace(STORAGE_DEVICE)>>10);//M
		DBG_PRINT("vfsFreeSpace(STORAGE_DEVICE)%dMB\r\n",vfsFreeSpace(STORAGE_DEVICE)>>20);//M
		if(freespace > (CARD_FULL_MB_SIZE << 20))
			freespace -= (CARD_FULL_MB_SIZE << 20);
		else
			freespace = 0;

		if(freespace == 0)
		{
			left_num = 0;
		}
		else
		{	
			
			if(1)		//last_pic_size == 0
				temp_pic_size = pic_size[ap_state_config_quality_get()][ap_state_config_pic_size_get()];
			else
				{
				temp_pic_size = last_pic_size;
				//DBG_PRINT("*****last_pic_size:%d ... \r\n", last_pic_size>>10);
				//DBG_PRINT("*****temp_pic_size:%d ... \r\n", temp_pic_size>>10);
				}
				
			//temp_pic_size = pic_size[1][ap_state_config_pic_size_get()];
				//DBG_PRINT("****TO this STEP111!vfsFreeSpace(STORAGE_DEVICE):%d\r\n****",vfsFreeSpace(STORAGE_DEVICE)>>10);//M
				//DBG_PRINT("*****temp_pic_size:%d ... \r\n", temp_pic_size);
			left_num = (freespace >> 10) / temp_pic_size;
			if(left_num > 99999)
				left_num = 99999;
		}
	}

	return left_num;
}


void ap_capture_left_num_clear(void)
{
	ap_state_handling_ASCII_str_draw_exit(&left_capture_num_str, 1);
}

void ap_capture_left_num_display(INT32U left_num)
{
	STRING_ASCII_INFO ascii_str;
	INT16U	tft_w = drv_l2_display_tft_width_get();
	INT16U	tft_h = drv_l2_display_tft_height_get();
	INT16S	pos_offset_x, pos_offset_y;
	INT16U	pos_x, pos_y;
	CHAR	digit[6] = { 0 };


	ap_capture_left_num_clear();

	digit[0] = (left_num / 10000) + 0x30;
	left_num %= 10000;

	digit[1] = (left_num / 1000) + 0x30;
	left_num %= 1000;

	digit[2] = (left_num / 100) + 0x30;
	left_num %= 100;

	digit[3] = (left_num / 10) + 0x30;
	left_num %= 10;

	digit[4] = left_num + 0x30;

	ascii_str.font_color = 0xffff;
	ascii_str.font_type = 0;
	ascii_str.pos_x = 0;
	ascii_str.pos_y = 0;
	ascii_str.str_ptr = digit;

	ascii_str.buff_w = ASCII_FONT_WIDTH * gp_strlen((INT8S*)ascii_str.str_ptr);
	ascii_str.buff_h = ASCII_FONT_HEIGHT;

	pos_offset_x = CAPTURE_LEFT_NUM_POS_X;
	if(pos_offset_x < 0)
	{
		pos_x = pos_offset_x + tft_w;
	}
	else
	{
		pos_x = pos_offset_x;
	}

	pos_offset_y = CAPTURE_LEFT_NUM_POS_Y;
	if(pos_offset_y < 0)
	{
		pos_y = pos_offset_y + tft_h;
	}
	else
	{
		pos_y = pos_offset_y;
	}

	left_capture_num_str.w = ascii_str.buff_w;
	left_capture_num_str.h = ascii_str.buff_h;

	// anchor from top-right of icon
	if(pos_x >= left_capture_num_str.w)
	{
		pos_x -= left_capture_num_str.w;
	}

	left_capture_num_str.pos_x = pos_x & ~0x1;
	left_capture_num_str.pos_y = pos_y & ~0x1;

	ap_state_handling_ASCII_str_draw(&left_capture_num_str, &ascii_str);
}

void ap_capture_resolution_clear(void)
{
	ap_state_handling_ASCII_str_draw_exit(&capture_resolution_str, 1);
}

void ap_capture_resolution_display(void)
{
	const CHAR *resv_str[] = {"12M", "10M", "8M", "5M", "3M", "2M", "1.3M", "VGA"};
	STRING_ASCII_INFO ascii_str;
	INT16U	tft_w = drv_l2_display_tft_width_get();
	INT16U	tft_h = drv_l2_display_tft_height_get();
	INT16S	pos_offset_x, pos_offset_y;
	INT16U	pos_x, pos_y;
	INT8U	temp;

	temp = ap_state_config_pic_size_get();

	if(temp >= (sizeof(resv_str) / sizeof(*resv_str)))
	{
		temp = (sizeof(resv_str) / sizeof(*resv_str)) - 1;
	}

	ascii_str.str_ptr = (CHAR*)resv_str[temp];
	ascii_str.font_color = 0xffff;
	ascii_str.font_type = 0;
	ascii_str.pos_x = 0;
	ascii_str.pos_y = 0;

	// +2 for draw font frame
	ascii_str.buff_w = 2 + ASCII_FONT_WIDTH * gp_strlen((INT8S*)ascii_str.str_ptr);
	ascii_str.buff_h = 2 + ASCII_FONT_HEIGHT;

	pos_offset_x = CAPTURE_RESV_POS_X;
	if(pos_offset_x < 0)
	{
		pos_x = pos_offset_x + tft_w;
	}
	else
	{
		pos_x = pos_offset_x;
	}

	pos_offset_y = CAPTURE_RESV_POS_Y;
	if(pos_offset_y < 0)
	{
		pos_y = pos_offset_y + tft_h;
	}
	else
	{
		pos_y = pos_offset_y;
	}

	capture_resolution_str.w = ascii_str.buff_w;
	capture_resolution_str.h = ascii_str.buff_h;

	// anchor from top-right of icon
	if(pos_x >= capture_resolution_str.w)
	{
		pos_x -= capture_resolution_str.w;
	}

	capture_resolution_str.pos_x = pos_x & ~0x1;
	capture_resolution_str.pos_y = pos_y & ~0x1;

	ap_state_handling_ASCII_str_draw(&capture_resolution_str, &ascii_str);
}

//add
void ap_capture_file_name_clear(void)
{
	ap_state_handling_ASCII_str_draw_exit(&capture_file_name_str, 1);
}


void ap_capture_file_name_display(INT8S* file_name, INT16U pos_x, INT16U pos_y, INT8U blink_enble)
{
	STRING_ASCII_INFO ascii_str;
	char file_path[50];
	INT16U	tft_w = drv_l2_display_tft_width_get();
	INT16U	tft_h = drv_l2_display_tft_height_get();
	INT16S	pos_offset_x, pos_offset_y;
	INT8U	temp;

	ap_capture_file_name_clear();
	sprintf(file_path, "%s", file_name);
	
	ascii_str.str_ptr = (CHAR*)file_path;
	ascii_str.font_color = 0xffff;
	ascii_str.font_type = 0;
	ascii_str.pos_x = 0;
	ascii_str.pos_y = 0;
	ascii_str.str_ptr[5] = '/';
	DBG_PRINT("ascii_str.str_ptr = %s\r\n", ascii_str.str_ptr);
	// +2 for draw font frame
	ascii_str.buff_w = 2 + ASCII_FONT_WIDTH * gp_strlen((INT8S*)ascii_str.str_ptr);
	ascii_str.buff_h = 2 + ASCII_FONT_HEIGHT;


	capture_file_name_str.w = ascii_str.buff_w;
	capture_file_name_str.h = ascii_str.buff_h;

	capture_file_name_str.pos_x = pos_x & ~0x1;
	capture_file_name_str.pos_y = pos_y & ~0x1;
	DBG_PRINT("aaaaa1111\r\n");
	ap_state_handling_ASCII_str_draw(&capture_file_name_str, &ascii_str);
	DBG_PRINT("bbb22222\r\n");
	if (blink_enble)
	{
		if (capture_show_file_cnt_start < 24)
			capture_show_file_cnt_start = 24;
		//msgQSend(PeripheralTaskQ, MSG_PERIPHERAL_TASK_CAPTURE_FILE_SHOW_START, NULL, 0, MSG_PRI_NORMAL);
		DBG_PRINT("ccc22222\r\n");
	}
}

extern INT16U present_state;
void ap_capture_file_name_get(INT8S* file_name) 
{
		STRING_ASCII_INFO ascii_str;
		INT16U size, i;
		CHAR spi_path[] = "FLASH/PHO00000.JPG";
#if C_SPI_FLASH_DISK
		if(ap_state_handling_storage_id_get() == NO_STORAGE)
		{	
			sprintf((char *) spi_path, (const char *) "FLASH/%s", (CHAR*)file_name);
		}
		else
		{
			ascii_str.str_ptr = (CHAR*)file_name;
		}
#else
		ascii_str.str_ptr = (CHAR*)file_name;
#endif
		
		ascii_str.font_color = 0xffff;
		ascii_str.font_type = 0;
		ascii_str.pos_x = 0;
		ascii_str.pos_y = 0;
		ascii_str.buff_w = CAPTURE_SHOW_FILE_NAME_BUFF_W;
		ascii_str.buff_h = CAPTURE_SHOW_FILE_NAME_BUFF_H;
		ascii_str.str_ptr[5] = '/';
		for(i = 0; i < CAPTURE_SHOW_FILE_NAME_BUFF_W * CAPTURE_SHOW_FILE_NAME_BUFF_H; ++i)
		{
			*((INT16U *) capture_show_file_name_buff + i) = TRANSPARENT_COLOR;
		}
		ap_state_resource_string_ascii_draw((INT16U *)capture_show_file_name_buff, &ascii_str, RGB565_DRAW);
		capture_show_file_cnt_start = 24;
}
void ap_capture_file_name_draw(INT16U* buff)
{
	if (capture_show_file_cnt_start && (present_state == STATE_PHOTO_CAPTURE))
	{
		DISPLAY_ICONSHOW icon = {CAPTURE_SHOW_FILE_NAME_BUFF_W, CAPTURE_SHOW_FILE_NAME_BUFF_H, TRANSPARENT_COLOR, 12, 30};
		ap_display_icon_draw(buff, (INT16U *)capture_show_file_name_buff, &icon);
	}
}
